package com.blb.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.entity.Order;
import com.blb.entity.OrderLog;
import com.blb.entity.ProcurementOrder;
import com.blb.entity.Sort;
import com.blb.mapper.ProcurementOrderMapper;
import com.blb.service.IOrderLogService;
import com.blb.service.IOrderService;
import com.blb.service.IProcurementOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tb
 * @since 2020-05-20
 */
@Service
public class ProcurementOrderServiceImpl extends ServiceImpl<ProcurementOrderMapper, ProcurementOrder> implements IProcurementOrderService {

    @Resource
    private ProcurementOrderMapper procurementOrderMapper;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderLogService orderLogService;

    @Override
    public List<Sort> sortTree(String id) {
        return procurementOrderMapper.sortTree(id);
    }

    @Override
    public List<ProcurementOrder> getProcurementOrder(String id, Integer start, Integer length, String pname, String status, Boolean zjdd, String cid) {
        return procurementOrderMapper.getProcurementOrder(id, start, length, pname, status, zjdd, cid);
    }

    @Override
    public int getProcurementOrderCount(String pname, String status, Boolean zjdd, String cid) {
        return procurementOrderMapper.getProcurementOrderCount(pname, status, zjdd, cid);
    }

    @Override
    public List<ProcurementOrder> orderProduct(String id, Integer start, Integer length, String sid, Integer sortType, String cid) {
        return procurementOrderMapper.orderProduct(id, start, length, sid, sortType,cid);
    }

    @Override
    public int orderProductCount(String sid, Integer sortType, String cid) {
        return procurementOrderMapper.orderProductCount(sid, sortType,cid);
    }

    @Transactional
    @Override
    public boolean buyProduct(Order o, String no, OrderLog orderLog) {
        boolean result = orderService.update(o, new UpdateWrapper<Order>().eq("order_id", no));
        if (result) {
            return orderLogService.save(orderLog);
        }
        return false;
    }

    @Transactional
    @Override
    public boolean buyProducts(Order o, String[] nos, List<OrderLog> list, Date date) {
        boolean result = orderService.update(o, new UpdateWrapper<Order>().in("order_id", nos));
        if (result) {
            return orderLogService.saveBatch(list);
        }
        return false;
    }

    @Transactional
    @Override
    public boolean takeProduct(Order o, String orderId, OrderLog orderLog) {
        boolean result = orderService.update(o, new UpdateWrapper<Order>().eq("order_id", orderId));
        if (result) {
            return orderLogService.save(orderLog);
        }
        return false;
    }
}
